<template>
  <div>
    <a-card :bordered="false" class="ant-pro-components-tag-select" v-show="!hiddenSearch">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="28">
            <a-col :md="5" :sm="12">
              <a-form-item label="车牌号">
                <a-input v-model="queryParam.plateNo" placeholder="请输入车牌号" @keydown.enter.native="searchEnterFun"/>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="写入状态码">
                <a-select placeholder="请选择" v-model="queryParam.writeDataStatus" @change="handleWriteDataStatusChange" @keydown.enter.native="searchEnterFun">
                  <a-select-option v-for="(action) in dictDataWriteDataStatus" :key="action.dictValue">
                    {{ action.dictLabel }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="发送状态">
                <a-select placeholder="请选择" v-model="queryParam.sendStatus" @change="handleSendStatusChange" @keydown.enter.native="searchEnterFun">
                  <a-select-option v-for="(action) in dictDataSendStatus" :key="action.dictValue">
                    {{ action.dictLabel }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="5" :sm="12">
              <a-form-item label="设备SN">
                <a-input v-model="queryParam.deviceNo" placeholder="请输入设备SN" @keydown.enter.native="searchEnterFun"/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="5" :sm="12">
                <a-form-item label="通道号">
                  <a-select placeholder="请选择通道号" v-model="queryParam.channelNo" @keydown.enter.native="searchEnterFun">
                    <a-select-option value="0">通道0</a-select-option>
                    <a-select-option value="2">通道2</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="5" :sm="12">
                <a-form-item label="公安备案号">
                  <a-input v-model="queryParam.keepDeviceNo" placeholder="请输入设备备案号" @keydown.enter.native="searchEnterFun"/>
                </a-form-item>
              </a-col>
              <a-col :md="7" :sm="10">
                <a-form-item label="违停时间">
                  <a-range-picker v-model="queryParam.illegalTime" @change="onChange" style="width: 100%"></a-range-picker>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="1" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-tooltip placement="topLeft" title="查询" arrow-point-at-center>
                  <a-button shape="circle" icon="search" @click="$refs.table.refresh(true)"/>
                </a-tooltip>
                <a-tooltip placement="topLeft" title="重置" arrow-point-at-center>
                  <a-button style="margin-left: 8px" @click="() => this.queryParam = {}" icon="sync"/>
                </a-tooltip>
                <a @click="toggleAdvanced" style="margin-left: 15px" >
                  <a-icon :style="{ fontSize: '20px',color: '#000000'}" :type="advanced ? 'fullscreen-exit' : 'fullscreen'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card
      style="margin-top: 10px"
      :bordered="false">
      <div class="table-operator">
        <a-tooltip placement="topLeft" title="隐藏/显示搜索栏" arrow-point-at-center>
          <a-button size="small" style="position: absolute;right: 55px;top: 8px;" shape="circle" :icon="icon" @click="hiddenSearchMenu(hiddenSearch)"/>
        </a-tooltip>
      </div>
      <s-table
        :scroll="{ x: 1500 }"
        ref="table"
        row-key="id"
        size="middle"
        :columns="columns"
        :data="loadData"
        :customRow="rowClick"
        showPagination="auto"
        :showTotal="true">
        <span slot="writeDataStatus" slot-scope="writeDataStatus">
          <a-tag :key="writeDataStatus" :color="writeDataStatus === '0' ? 'gray': writeDataStatus === '1' ? 'red' : 'green'">
            {{ writeDataStatus === '0'? '未写入': writeDataStatus === '1' ? '失败' : '成功' }}
          </a-tag>
        </span>
        <span slot="sendStatus" slot-scope="sendStatus">
          <a-tag :key="sendStatus" :color="sendStatus === '0' ? 'gray': sendStatus === '1' ? 'red' : 'green'">
            {{ sendStatus === '0'? '未发送': sendStatus === '1' ? '失败' : '成功' }}
          </a-tag>
        </span>
      </s-table>
      <remind-details ref="drawer"/>
    </a-card>
  </div>
</template>

<script>
import moment from 'moment'
import {
  dictDataList, findIllegalRemindPage
} from '@/api/system'
import { STable } from '@/components'
import RemindDetails from '../modules/RemindDetails'
export default {
  components: {
    STable,
    RemindDetails
  },
  name: 'Remind',
  data () {
    return {
      // 查询参数
      queryParam: {},
      advanced: false,
      icon: 'eye-invisible',
      hiddenSearch: false,
      selectedRowKeys: [],
      selectedRows: [],
      dictDataSendStatus: [],
      dictDataWriteDataStatus: [],
      // 表头
      columns: [
        {
          title: '车牌号',
          dataIndex: 'plateNo',
          align: 'center',
          width: 100,
          fixed: 'left'
        },
        {
          title: '设备SN',
          dataIndex: 'deviceNo',
          align: 'center',
          width: 120
        },
        {
          title: '通道号',
          dataIndex: 'channelNo',
          align: 'center',
          width: 100
        },
        {
          title: '设备名称',
          dataIndex: 'deviceName',
          align: 'center',
          width: 180
        },
        {
          title: '违停时间',
          dataIndex: 'illegalTime',
          align: 'center',
          width: 180,
          ellipsis: true,
          customRender: (text, row, index) => {
            return moment(Number(text)).format('YYYY-MM-DD HH:mm:ss')
          }
        },
        {
          title: '违停写入状态',
          dataIndex: 'writeDataStatus',
          align: 'center',
          scopedSlots: { customRender: 'writeDataStatus' },
          width: 100
        },
        {
          title: '违停写入时间',
          dataIndex: 'writeDataTime',
          align: 'center',
          width: 180,
          sorter: true
        },
        {
          title: '短信发送状态',
          dataIndex: 'sendStatus',
          align: 'center',
          scopedSlots: { customRender: 'sendStatus' },
          width: 100
        },
        {
          title: '短信发送时间',
          dataIndex: 'sendRemindTime',
          align: 'center',
          width: 180
        },
        {
          title: '上传序号',
          dataIndex: 'uploadIndex',
          align: 'center',
          width: 180
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center',
          width: 180
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return findIllegalRemindPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      }
    }
  },
  created () {
    this.loadDictWriteDataStatus()
    this.loadDictDataSendStatus()
  },
  filters: {
  },
  methods: {
    searchEnterFun (e) {
      const keyCode = window.event ? e.keyCode : e.which
      if (keyCode === 13) {
        this.$refs.table.refresh(true)
      }
    },
    rowClick: function (record, index) {
      return {
        on: {
          dblclick: () => {
            this.$refs.drawer.details(record)
          }
        }
      }
    },
    onChange (date, dateString) {
      this.queryParam.beginTime = dateString[0]
      this.queryParam.endTime = dateString[1] + ' 23:59:59'
      this.$refs.table.refresh(true)
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    handleWriteDataStatusChange (value) {
      this.queryParam.writeDataStatus = value
    },
    handleSendStatusChange (value) {
      this.queryParam.sendStatus = value
    },
    loadDictWriteDataStatus () {
      dictDataList({ dictType: 'write_data_status' }).then(res => {
        this.dictDataWriteDataStatus = res.data
      })
    },
    loadDictDataSendStatus () {
      dictDataList({ dictType: 'send_status' }).then(res => {
        this.dictDataSendStatus = res.data
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hiddenSearchMenu (hiddenSearch) {
      this.hiddenSearch = hiddenSearch !== true
      this.icon = this.hiddenSearch === true ? 'eye' : 'eye-invisible'
    }
  }
}
</script>

<style lang="less" scoped>
  /deep/.ant-form-item-control {
    position: relative;
    line-height: 30px;
    zoom: 1;
  }
</style>
